package com.example.rabc.controller;

import com.example.rabc.common.ApiResponse;
import com.example.rabc.entity.Permission;
import com.example.rabc.service.PermissionComponent;
import com.example.rabc.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限控制器
 */
@RestController
@RequestMapping("/api/permissions")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;  // 权限服务

    /**
     * 创建权限
     * @param permission 权限对象
     * @return 包含创建权限的ApiResponse对象
     */
    @PostMapping
    public ResponseEntity<ApiResponse<Permission>> createPermission(@RequestBody Permission permission) {
        Permission createdPermission = permissionService.createPermission(permission);
        return ResponseEntity.ok(ApiResponse.success(createdPermission));
    }

    /**
     * 更新权限
     * @param permissionId 权限ID
     * @param permission 权限对象
     * @return 包含更新权限的ApiResponse对象
     */
    @PutMapping("/{permissionId}")
    public ResponseEntity<ApiResponse<Permission>> updatePermission(@PathVariable Long permissionId, 
                                                                    @RequestBody Permission permission) {
        Permission updatedPermission = permissionService.updatePermission(permissionId, permission);
        return ResponseEntity.ok(ApiResponse.success(updatedPermission));
    }

    /**
     * 删除权限
     * @param permissionId 权限ID
     * @return 包含空响应体的ApiResponse对象
     */
    @DeleteMapping("/{permissionId}")
    public ResponseEntity<ApiResponse<Void>> deletePermission(@PathVariable Long permissionId) {
        permissionService.deletePermission(permissionId);
        return ResponseEntity.ok(ApiResponse.success(null));
    }

    /**
     * 获取权限详情
     * @param permissionId 权限ID
     * @return 包含权限详情的ApiResponse对象
     */
    @GetMapping("/{permissionId}")
    public ResponseEntity<ApiResponse<Permission>> getPermissionById(@PathVariable Long permissionId) {
        Permission permission = permissionService.getPermissionById(permissionId);
        return ResponseEntity.ok(ApiResponse.success(permission));
    }

    /**
     * 根据权限代码获取权限详情
     * @param permissionCode 权限代码
     * @return 包含权限详情的ApiResponse对象
     */
    @GetMapping("/code/{permissionCode}")
    public ResponseEntity<ApiResponse<Permission>> getPermissionByCode(@PathVariable String permissionCode) {
        Permission permission = permissionService.getPermissionByCode(permissionCode);
        return ResponseEntity.ok(ApiResponse.success(permission));
    }

    /**
     * 获取所有权限
     * @return 包含所有权限的ApiResponse对象
     */
    @GetMapping
    public ResponseEntity<ApiResponse<List<Permission>>> getAllPermissions() {
        List<Permission> permissions = permissionService.getAllPermissions();
        return ResponseEntity.ok(ApiResponse.success(permissions));
    }

    /**
     * 根据资源类型获取权限列表
     * @param resourceType 资源类型
     * @return 包含权限列表的ApiResponse对象
     */
    @GetMapping("/resource-type/{resourceType}")
    public ResponseEntity<ApiResponse<List<Permission>>> getPermissionsByResourceType(@PathVariable String resourceType) {
        List<Permission> permissions = permissionService.getPermissionsByResourceType(resourceType);
        return ResponseEntity.ok(ApiResponse.success(permissions));
    }

    /**
     * 获取权限树结构
     * @param rootPermissionId 根权限ID，如果为null则构建整棵树
     * @return 包含权限树结构的ApiResponse对象
     */
    @GetMapping("/tree")
    public ResponseEntity<ApiResponse<PermissionComponent>> getPermissionTree(
            @RequestParam(required = false) Long rootPermissionId) {
        PermissionComponent permissionTree = permissionService.getPermissionTree(rootPermissionId);
        return ResponseEntity.ok(ApiResponse.success(permissionTree));
    }

    /**
     * 检查用户是否具有指定权限
     * @param userId 用户ID
     * @param permissionCode 权限代码
     * @return 包含检查结果的ApiResponse对象
     */
    @GetMapping("/check-user-permission")
    public ResponseEntity<ApiResponse<Boolean>> checkUserPermission(
            @RequestParam Long userId,
            @RequestParam String permissionCode) {
        boolean hasPermission = permissionService.checkUserPermission(userId, permissionCode);
        return ResponseEntity.ok(ApiResponse.success(hasPermission));
    }

    /**
     * 获取用户权限树
     * @param userId 用户ID
     * @return 包含用户权限树的ApiResponse对象
     */
    @GetMapping("/user/{userId}/tree")
    public ResponseEntity<ApiResponse<List<PermissionComponent>>> getUserPermissionTree(@PathVariable Long userId) {
        List<PermissionComponent> userPermissionTree = permissionService.getUserPermissionTree(userId);
        return ResponseEntity.ok(ApiResponse.success(userPermissionTree));
    }
}